package com.base.cn.platform.os.controller.manage.teacher;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.log.aop.AdminModuleEnum;
import com.base.cn.platform.os.common.log.aop.OpLog;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.service.course.qa.question.QaQuestionService;
import com.base.cn.platform.os.service.course.qa.record.QaGainAttendRecordService;
import com.base.cn.platform.os.service.manage.sysUser.SysLoginLogService;
import com.base.cn.platform.os.service.manage.teacher.SysTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
/**
 * 后台管理讲师Controller
 *
 * @author s.li
 * @create 2018-04-08-16:34
 */
@Controller
@RequestMapping("/admin/manage/teacher")
public class AdminTeacherController extends AdminBaseController{

    @Autowired
    private SysTeacherService sysTeacherService;
    @Autowired
    private QaQuestionService qaQuestionService;
    @Autowired
    private SysLoginLogService sysLoginLogService;
    @Autowired
    private QaGainAttendRecordService qaGainAttendRecordService;

    /**
     * 显示讲师列表
     * @param model
     * @return 列表页面
     */
    @RequestMapping("/listTeacher")
    public String listTeacher(Model model){
        Map<String,Object> params = new HashMap<>();
        BigDecimal count = sysTeacherService.countTeacher(params);
        model.addAttribute("count",count);
        return resultPage("/manage/teacher/teacher-list");
    }

    /**
     * 查询分页讲师数据
     * @param model
     * @param params 查询参数
     * @param currentPage 页码
     * @return 数据列表
     */
    @RequestMapping("/ajaxQueryTeacherList")
    public String ajaxQueryTeacherList(Model model,
                                       @RequestBody Map<String,Object> params,
                                       @RequestParam(value = "currentPage",required = false,defaultValue = "1") int currentPage){
        Map<String,Object> dataMap = sysTeacherService.findSysTeacherListPage(params,currentPage,true,true,true);
        model.addAttribute("dataMap",dataMap);
        return resultPage("/manage/teacher/ajax-teacher-list");
    }

    /**
     * 后台查询讲师详情页面
     * @param model Model
     * @param teacherId 讲师ID
     * @return String
     */
    @RequestMapping("/teacherInfo")
    public String teacherInfo(Model model,
                              @RequestParam("teacherId") BigDecimal teacherId,
                              @RequestParam(value = "flag", required = false, defaultValue = "false") boolean flag){
        Map<String, Object> teacher = sysTeacherService.findSysTeacherById(teacherId, flag, flag, flag, false, flag);
        model.addAttribute("teacher", teacher);
        if(!flag){
            return resultPage("/manage/teacher/info/teacher-info");
        }else {
            return resultPage("/manage/teacher/info/teacher-info-basic");
        }
    }

    /**
     * 查询讲师回答记录
     * @param model Model
     * @param currentPage 页码
     * @param params 条件
     * @return String
     */
    @RequestMapping("/findQaAnswerRecordList")
    public String findQaAnswerRecordList(Model model,
                                         @RequestParam(value = "currentPage", required = false, defaultValue = "1") int currentPage,
                                         @RequestBody Map<String, Object> params){
        model.addAttribute("startTime", params.get("replyStartTime"));
        model.addAttribute("endTime", params.get("replyEndTime"));

        params.put("completeStatus", 2);
        params.put("selectFrom", 1);
        Map<String, Object> dataMap = qaQuestionService.queryQuestionListPage(currentPage, 10, params, null, false, false, true, false, false, false, true,false);
        model.addAttribute("dataMap", dataMap);
        return resultPage("/manage/teacher/info/teacher-info-answer");
    }

    /**
     * 查询问题围观收益记录
     * @param model Model
     * @param currentPage 页码
     * @param params 查询条件
     * @return String
     */
    @RequestMapping("/findGainAttendRecordList")
    public String findGainAttendRecordList(Model model,
                                           @RequestParam(value = "currentPage", required = false, defaultValue = "1") int currentPage,
                                           @RequestBody Map<String, Object> params){
        model.addAttribute("startTime", params.get("beginTime"));
        model.addAttribute("endTime", params.get("endTime"));
        params.put("userType", 2);
        Map<String, Object> dataMap = qaGainAttendRecordService.queryGainAttendRecordListPage(currentPage, params, false, true);
        model.addAttribute("dataMap", dataMap);
        return resultPage("/manage/teacher/info/teacher-info-attendEarning");
    }

    /**
     * 查询讲师登录记录
     * @param model Model
     * @param currentPage 页码
     * @param params 查询条件
     * @return
     */
    @RequestMapping("/findLoginLogList")
    public String findLoginLogList(Model model,
                                   @RequestParam(value = "currentPage", required = false, defaultValue = "1") int currentPage,
                                   @RequestBody Map<String, Object> params){
        model.addAttribute("startTime", params.get("startTime"));
        model.addAttribute("endTime", params.get("endTime"));
        Map<String, Object> dataMap = sysLoginLogService.findSysLoginListPage(params, currentPage, true);
        model.addAttribute("dataMap", dataMap);
        return resultPage("/manage/teacher/info/teacher-info-login-log");
    }


    /**
     * 初始化修改或添加讲师页面
     * @param model
     * @param id 讲师ID（用于修改，添加时可不传）
     * @return 操作页面
     */
    @RequestMapping(value = {"/initUpdate","/initAdd"})
    public String initUpdateOrAdd(Model model,
                                  @RequestParam(value = "id",required = false,defaultValue = "0") BigDecimal id){
        Map<String,Object> sysTeacher = null;
        if(DataUtil.idIsNotNull(id)){
            sysTeacher = sysTeacherService.findSysTeacherById(id,true,false,true, false, true);
        }
        if(ObjectUtils.isEmpty(sysTeacher)){
            sysTeacher = new HashMap<>();
        }
        model.addAttribute("teacher",sysTeacher);
        return resultPage("/manage/teacher/save-teacher");
    }

    /**
     * 保存讲师数据
     * @param request HttpServletRequest
     * @param sysTeacher 要保存讲师数据对象
     * @return Map<String,Object>
     */
    @RequestMapping("/saveSysTeacher")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.SYS_USER,action = "保存或修改讲师数据")
    public Map<String,Object> saveSysTeacher(HttpServletRequest request,
                                             @RequestBody Map<String, Object> sysTeacher){
        sysTeacher.put("createUserId",loginUtil.getLoginUserId(request));
        return sysTeacherService.saveSysTeacher(sysTeacher);
    }


    /**
     * 修改讲师状态
     * @param ids 讲师ID串
     * @param status 状态，1正常，2禁用，3删除
     * @return Map<String,Object>
     */
    @RequestMapping("/updateSysTeacherStatus")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.SYS_USER,action = "修改讲师状态")
    public Map<String,Object> updateSysTeacherStatus(@RequestParam("ids") String ids,@RequestParam("status") int status){
        return sysTeacherService.updateSysTeacherStatus(ids,status);
    }

    /**
     * 显示选择讲师列表
     * @param model
     * @return 列表页面
     */
    @RequestMapping("/selectTeacherList")
    public String selectTeacherList(Model model,
                                    @RequestParam(value = "subjectIds",required = false) String subjectIds,
                                    @RequestParam(value = "selectType",required = false,defaultValue = "radio") String selectType,
                                    @RequestParam(value = "teacherIdentity",required = false) String teacherIdentity){
        model.addAttribute("selectType",selectType);
        model.addAttribute("subjectIds",subjectIds);
        model.addAttribute("teacherIdentity",teacherIdentity);
        return resultPage("/manage/teacher/select-teacher-list");
    }

    /**
     * 查询分页讲师数据
     * @param model
     * @param params 查询参数
     * @param currentPage 页码
     * @return 数据列表
     */
    @RequestMapping("/ajaxSelectTeacherList")
    public String ajaxSelectTeacherList(Model model,
                                       @RequestBody Map<String,Object> params,
                                       @RequestParam(value = "currentPage",required = false,defaultValue = "1") int currentPage){
        params.put("status",1);
        Map<String,Object> dataMap = sysTeacherService.findSysTeacherListPage(params,currentPage,false,false,false);
        model.addAttribute("dataMap",dataMap);
        model.addAttribute("selectType",params.get("selectType"));
        return resultPage("/manage/teacher/select-ajax-teacher-list");
    }
}
